<template>
  <div class="app-container">
    <el-row class="library-row">
      <el-col :span="3" :offset="21" class="add">
        <el-button @click="addBooks" type="primary" icon="el-icon-plus">添加权限</el-button>
      </el-col>
    </el-row>
     <el-table
    :data="tableData"
    style="width: 100%;margin-bottom: 20px;"
    row-key="menu_id"
    stripe
    default-expand-all
    :tree-props="{children: 'list', hasChildren: 'hasList'}">

    <el-table-column prop="menu_name" label="权限名称"></el-table-column>
     <el-table-column prop="menu_url" label="菜单URL"></el-table-column>
        <el-table-column fixed="right" label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editor(scope.row)">编辑</el-button>
          <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
  </el-table>
    <el-row class="total">
    </el-row>

    <!-- 弹窗 -->
    <el-dialog :title="title" class="school" :close-on-click-modal="false"  width="25%" :visible.sync="dialogFormVisible">
      <el-form :model="formData" ref="formSave" :rules="rules">
         <el-row>
           <el-col :span="12">
              <el-form-item  label="上级目录" prop="menu_pid">
                <el-cascader
                   placeholder="请选择"
                    :options="menus"
                    v-model="formData.menu_pid"
                    :props="prop"
                    clearable></el-cascader>
          </el-form-item>
           </el-col>
           <el-col :span="12">
                <el-form-item label="菜单排序" prop="menu_order">
          <el-input-number v-model="formData.menu_order" controls-position="right"  :min="0"  autocomplete="off"></el-input-number>
        </el-form-item>
           </el-col>
         </el-row>
        <el-form-item label="菜单名称" prop="menu_name">
          <el-input v-model="formData.menu_name" placeholder="请填写" autocomplete="off"></el-input>
        </el-form-item>
           <el-form-item label="菜单后端地址" prop="menu_url">
          <el-input v-model="formData.menu_url" placeholder="请填写" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="菜单Path" prop="path">
          <el-input v-model="formData.path" placeholder="请填写" autocomplete="off"></el-input>
        </el-form-item>
          <el-form-item label="菜单模块名称" prop="component">
          <el-input v-model="formData.component" placeholder="请填写" autocomplete="off"></el-input>
        </el-form-item>
             <el-form-item label="菜单图标" prop="icon">
          <el-input v-model="formData.icon" placeholder="请填写" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单状态" >
           <el-radio-group v-model="formData.status">
    <el-radio :label="0">禁用</el-radio>
    <el-radio :label="1">启用</el-radio>
 
  </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save('formSave')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  menuList,
  addMenuList,
  delMenuList,
  getMenuLeveList,
  getMenuLeve,
  editorMenuList,
  sortMenu,
  statusMenu
} from "@/api/set";

export default {
  data() {
    return {
      title: "添加权限",
      total: 1,
      systemNodeFlag: false,
      isEdit: false,
      form: {
        page: 1,
        page_size: 20,
        menu_name: "",
        sort: "",
        order: "",
      },
      formData: {
        menu_name: "",
        menu_url:'',
        menu_pid:null,
        menu_order:'0',
        icon:'',
        status:null,
        path:'',
        component:''
      },
      menus:[],
      prop:{
          value:'menu_id',
          label:'menu_name',
          checkStrictly: true,
          children:'list'
      },
      rules: {
         menu_pid: [
          { required: true, message: "请填写上级", trigger: "change" },
        ],
        menu_name: [
          { required: true, message: "请填写权限名称", trigger: "blur" },
        ],
        menu_url: [
          { required: true, message: "请填写后端路径", trigger: "blur" },
        ]
      },
      id: null,
      dialogFormVisible: false, //新增弹窗
      list: null,
      listLoading: true,
      value: "",
      tableData: [],
      data: [],
      defaultProps: {
        children: "list",
        label: "menu_name",
      },
    };
  },
  mounted() {
    this.fetchData();
    this.getNode();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      menuList().then((response) => {
        this.total = response.total;
        this.tableData = response.data;
      });
    },
    getNode() {
      getMenuLeveList().then((response) => {
        let data = response.data;
        data.unshift({menu_id:0,menu_name:'一级菜单',menu_pid:0})
        
        this.menus = data;
         
      });
    },
    // 删除请求
    async delete() {
      let $id = this.id;
      let del = await delMenuList($id);
      if (del.code == 1) {
        this.$message({
          type: "success",
          message: del.message,
        });
      } else {
        this.$message({
          type: "info",
          message: del.message,
        });
      }
      this.fetchData();
    },
    // 保存新增请求
    async saveData() {
      let data = {
        Menu_name:this.formData.Menu_name,
        menuids:this.formData.menuids
      }
      let res = await addMenuList(data);
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.fetchData();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 保存编辑请求
    async saveDataEditor() {
      let $id = this.id;
      let data = this.formData;
      let res = await editorMenuList(data, $id);
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.fetchData();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 获取编辑请求
    async editorData() {

      let $id = this.id;
      let res = await getMenuLeve($id);
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
    this.$nextTick(()=>{
  this.formData=res.data

    })

      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 添加图书
    addBooks() {
 this.formData={}
      this.dialogFormVisible = true;
      this.title = "添加权限";
      this.formData = {};
    },
    // 编辑
    editor(row) {
      console.log(row);
      this.id = row.menu_id;
      this.dialogFormVisible = true;
      this.title = "编辑权限";
      this.editorData();
    },
    // 删除
    del(row) {
      this.id = row.menu_id;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delete();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 排序
    sort(val) {
      let order = val.order;
      if (order == "descending") {
        this.form.order = "desc";
      } else if (order == "ascending") {
        this.form.order = "asc";
      } else {
        this.form.order = null;
      }
      this.changedata(this.form.order);
    },
    // 页签
    currentPage(val) {
      this.form.page = val;
      this.changedata();
    },
    // 保存数据
    save(formSave) {
      if (this.title == "添加权限") {
        this.saveData();
        console.log(1);
      } else {
        this.saveDataEditor();
        console.log(2);
      }
    },
  },
};
</script>
<style>
.library-row {
  padding: 15px 0;
}
.add {
  display: flex;
  justify-content: flex-end;
}
.el-dialog__body {
  border: 1px solid #ddd;
}
.total {
  display: flex;
  justify-content: center;
  padding: 40px 0;
}
.el-form-item__label {
  float: none;
}
.school .el-select {
  display: inline-block;
  width: 100%;
  position: relative;
}
.el-tree-node__children .el-tree-node__children > div {
  float: left;
}
</style>